package com.wspyydshhh.mytool.slice;

import com.wspyydshhh.mytool.ResourceTable;
import com.wspyydshhh.mytool.pojo.ConstData;
import com.wspyydshhh.mytool.util.DBUtil;
import com.wspyydshhh.mytool.util.DataUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.AbilityContext;
import ohos.data.distributed.user.SingleKvStore;

import java.util.Calendar;
import java.util.UUID;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class AddMainPageSlice extends AbilitySlice {
    private int year = -1, month = -1, day = -1,hours = -1,mimute = -1,second = -1;
    private long time;
    private String title,remarks,operationName;
    private double operationNum;
    private Text selectedDateText,selectedTimeText;
    private Component returnBack,liuzhuang,add,chooseTime,choose_day;
    private TextField inputTitle,inputFunds,inputUser,inputOther;
    private SingleKvStore singleKvStore = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_add_main_page);
        chooseTime = findComponentById(ResourceTable.Id_choose_changeTime);
        choose_day = findComponentById(ResourceTable.Id_choose_changeDay);
        inputTitle = (TextField) findComponentById(ResourceTable.Id_Title);
        inputFunds = (TextField) findComponentById(ResourceTable.Id_funds);
        inputUser = (TextField) findComponentById(ResourceTable.Id_user_add);
        inputOther = (TextField) findComponentById(ResourceTable.Id_Other);
        selectedDateText = (Text) findComponentById(ResourceTable.Id_show_day);
        selectedTimeText = (Text) findComponentById(ResourceTable.Id_show_time);
        returnBack = findComponentById(ResourceTable.Id_returnBack);
        liuzhuang = findComponentById(ResourceTable.Id_liuzhuang);
        add = findComponentById(ResourceTable.Id_create);
        singleKvStore = DBUtil.initOrGetDB(this, ConstData.dataBaseId);
        add.setClickedListener(this::onClick);
        choose_day.setClickedListener(this::onClick);
        chooseTime.setClickedListener(this::onClick);
        returnBack.setClickedListener(this::onClick);
        liuzhuang.setClickedListener(this::onClick);
    }
    public void onClick(Component component) {
        int id = component.getId();
        switch (id) {
            case ResourceTable.Id_choose_changeDay: {
                showPickDateConfirmTips(this);
                break;
            }
            case ResourceTable.Id_choose_changeTime: {
                showPickTimeConfirmTips(this);
                break;
            }
            case ResourceTable.Id_create:{
                if(year<0||hours<0) {
                    return;
                }
                time = DataUtil.getTimestamOfDate(year,month,day,hours,mimute,second);
                title = inputTitle.getText();
                String inputNum = inputFunds.getText();
                remarks = inputOther.getText();
                operationName = inputUser.getText();
                if(title==null||operationName==null||remarks==null||inputNum==null) {
                    return;
                }
                if(!inputNum.matches("^[0-9]{1,9}.[0-9]{2}$")) {
                    return;
                }
                operationNum = Double.parseDouble(inputNum);
                if(title.length()<1||title.length()>10||operationName.length()<1||operationName.length()>10||remarks.length()<1||remarks.length()>200) {
                    return;
                }
                if(year<Calendar.getInstance().get(Calendar.YEAR)) {
                    return;
                }
                if(!title.matches(ConstData.matchCommonText)||!operationName.matches(ConstData.matchCommonText)||!remarks.matches(ConstData.matchCommonText)) {
                    return;
                }
                String nodeId;
                nodeId= UUID.randomUUID().toString();
                System.out.println(time+" "+title+" "+operationNum+" "+operationName+" "+remarks+" ");
                try {
                    singleKvStore.putString(nodeId,"{\"id\":\""+nodeId+
                            "\",\"title\":\""+title+
                            "\",\"year\":"+year+
                            ",\"month\":"+month+
                            ",\"day\":"+day+
                            ",\"times\":"+time+
                            ",\"operationNum\":"+operationNum+
                            ",\"remarks\":\""+remarks+
                            "\",\"operationName\":\""+operationName+
                            "\",\"hasTask\":"+0+
                            ",\"isAdd\":"+false+"}");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    inputOther.setText(null);
                    inputUser.setText(null);
                    inputTitle.setText(null);
                    inputFunds.setText(null);
                    selectedDateText.setText(null);
                    selectedTimeText.setText(null);
                    terminate();
                }
                break;
            }
            case ResourceTable.Id_returnBack:{
                inputOther.setText(null);
                inputUser.setText(null);
                inputTitle.setText(null);
                inputFunds.setText(null);
                selectedDateText.setText(null);
                selectedTimeText.setText(null);
                terminate();
                break;
            }
            case ResourceTable.Id_liuzhuang:{

                break;
            }
            default: break;
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public void showPickDateConfirmTips(AbilityContext context) {
        CommonDialog commonDialog = new CommonDialog(context);
        Component rootView = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_dialog_date_layout_confirm, null, false);
        DatePicker datePicker = (DatePicker) rootView.findComponentById(ResourceTable.Id_date_pick);
        Text confirm = (Text) rootView.findComponentById(ResourceTable.Id_dialog_confirm);
        confirm.setClickedListener(c -> {
            year = datePicker.getYear();
            month = datePicker.getMonth();
            day = datePicker.getDayOfMonth();
            selectedDateText.setText(year + "年" + month + "月" + day + "日");
            commonDialog.remove();
        });
        commonDialog.setSize(MATCH_PARENT, MATCH_CONTENT);
        commonDialog.setAlignment(LayoutAlignment.CENTER);
        commonDialog.setTransparent(true);
        commonDialog.setCornerRadius(15);
        commonDialog.setContentCustomComponent(rootView);
        commonDialog.show();
    }

    public void showPickTimeConfirmTips(AbilityContext context) {
        CommonDialog commonDialog = new CommonDialog(context);
        Component rootView = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_dialog_time_layout_confirm, null, false);
        TimePicker timePicker = (TimePicker) rootView.findComponentById(ResourceTable.Id_time_pick);
        Text confirm = (Text) rootView.findComponentById(ResourceTable.Id_dialog_confirm);
        confirm.setClickedListener(c -> {
            hours = timePicker.getHour();
            mimute = timePicker.getMinute();
            second = timePicker.getSecond();
            selectedTimeText.setText(hours+":"+mimute+":"+second);
            commonDialog.remove();
        });
        commonDialog.setSize(MATCH_PARENT, MATCH_CONTENT);
        commonDialog.setAlignment(LayoutAlignment.CENTER);
        commonDialog.setTransparent(true);
        commonDialog.setCornerRadius(15);
        commonDialog.setContentCustomComponent(rootView);
        commonDialog.show();
    }
}
